package login;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Home-pc
 * Date: 2023-10-27
 * Time: 14:08
 */
//生成动态的主页面，从服务器获得信息
@WebServlet("/index")
public class IndexServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //此处查看用户是否已经登录，即Session中是否有username
        //先获得所有的session，在去这些session中找username
        //禁止创建会话
        HttpSession session=req.getSession(false);
        if(session==null){
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("用户未登录");
            return;
        }
        String username=(String) session.getAttribute("username");
        // 虽然有会话对象, 但是里面没有必要的属性, 也认为是登录状态异常.
        if(username==null){
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("用户未登录");
            return;
        }
        //如果上述ok，则生成一个动态的画面
        resp.setContentType("text/html;charset=utf8");
        resp.getWriter().write("欢迎 " + username);
    }
}
